package com.cheers.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import com.cheers.dao.IntegralDao;
import com.cheers.model.Integral;

public class IntegralDaoImpl extends AbstractDaoImpl implements IntegralDao {

	@Override
	public List<Object[]> rank(int firstIndex, int pageSize)
			throws Exception {
		String sql = "select u.id,u.username,ig.sump from user_info u, (select ii.userId,sum(ii.point) as sump from integral_info ii group by userId) ig where u.id=ig.userId order by ig.sump desc";
		List<Object[]> result = findBySql(firstIndex, pageSize, sql, null);
		return result;
	}

	@Override
	public Integral getIntegral(String userId, String channel) throws Exception {
		Integral result = null;
		String sql = "from Integral where userId=? and channel=?";
		Object[] params = new Object[] {userId, channel};
		result = findByHql(Integral.class, sql, params);
		return result;
	}

	@Override
	public Long getTotalPoint(String userId) throws Exception {
		Long result = null;
		String sql = "select sum(point) from integral_info where userId=? group by userId";
		Object[] params = new Object[] {userId};
		BigDecimal total = getSum(sql, params);
		if (total == null) return result; 
		result = total.longValue();
		return result;
	}

}
